package com.itita.ww2.handler.requesthandler.worldmap;

import java.sql.SQLException;

import com.smartfoxserver.v2.entities.User;
import com.smartfoxserver.v2.entities.data.ISFSObject;
import com.smartfoxserver.v2.entities.data.SFSObject;
import com.itita.ww2.core.MessageHandler;
import com.itita.ww2.core.WW2Exception;
import com.itita.ww2.game.user.UserService;
import com.itita.ww2.game.worldmap.WorldMapManager;
import com.itita.ww2.game.worldmap.WorldMapService;
import com.itita.ww2.model.user.UserProfile;
import com.itita.ww2.model.worldmap.Base;
import com.itita.ww2.utils.handler.IRequestOper;

/**
 * 据点强化捐献
 * @author NorthLan
 *
 */
public class BaseEnDonate implements IRequestOper {

	@Override
	public boolean execute(String requestCMD, User user, ISFSObject params) throws WW2Exception, SQLException {
		UserProfile userProfile = UserService.getInstance().getUserProfile(user);
		ISFSObject donateObj = params.getSFSObject("donate");
		int blockId = params.getInt("blockId");
		int cityId = params.getInt("cityId");
		int baseId = params.getInt("baseId");
		// 
		if (!WorldMapService.baseEnhanceDonate(userProfile, donateObj, blockId, cityId, baseId)) {
			throw new WW2Exception();
		}
		ISFSObject backObj = SFSObject.newInstance();
		// 获取据点当前升级资源
		Base base = WorldMapManager.getInstance().getBase(blockId, cityId, baseId);
		if (base.canUpgrade()) {
			ISFSObject upObj = SFSObject.newInstance();
			upObj.putSFSObject("need", base.getUpgradeResource());
			upObj.putSFSObject("donate", base.getUpgradeDonateResourceObj());
			backObj.putSFSObject("upgradeRes", upObj);
		}
		// 获取据点当前强化资源
		if (base.canEnhance()) {
			ISFSObject enObj = SFSObject.newInstance();
			enObj.putSFSObject("need", base.getEnhanceResource());
			enObj.putSFSObject("donate", base.getEnhanceDonateResourceObj());
			backObj.putSFSObject("enhanceRes", enObj);
		}
		MessageHandler.getInstance().sendMsg(requestCMD, backObj, user, null, true);
		return true;
	}

}
